﻿using ERPPublic;
using FinancialManagement.Application.Command.ActualRefundCommand;
using FinancialManagement.Application.Command.ReceivableCommand;
using FinancialManagement.Domain.Dto;
using FinancialManagement.Domain.Model;
using FinancialManagement.Dtos;
using FinancialManagement.Infrstructre;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FinancialManagement.Controllers
{
    /// <summary>
    /// 应收管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class ReceivableController : ControllerBase
    {

        protected readonly IMediator mediatorp;
        public ReceivableController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }

        /// <summary>
        /// 应收列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<ListReceivableDto>>> GetReceivableList([FromQuery] ListReceivableCommand command)
        {
            ApiResult<PageDto<ListReceivableDto>> result = new ApiResult<PageDto<ListReceivableDto>>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填（直接退款）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<SaleContract>> FanReceivable([FromQuery] FanReceivableNumberCommand command)
        {
            ApiResult<SaleContract> result = new ApiResult<SaleContract>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 添加（直接退款）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddReceivable(AddReceivableCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }




    }
}
